import hall
import cPickle, util, pyrpc
from bsddb import db

class ReadState:
    def __init__ (self):
        self.readst = db.DB()
        self.readst.open (hall.dbconf['READ.ST'],
                          dbtype = db.DB_HASH, flags = db.DB_CREATE)

    def addUnread (self, docID):
        self.readst.put(str(docID), '1')

    def setRead (self, docID):
        try:
            self.readst.delete('%s' % docID)
            return True
        except:
            return False

    def isUnread (self, docID):
        if (self.readst.get('%s' % docID)):
            return True
        else:
            return False


if __name__ == '__main__':
    util.setLogging('readst.log')    
    util.initSignal()
    readSt = ReadState()
    server = pyrpc.rpc.RPCServer()
    server.bindFunction(readSt.addUnread)
    server.bindFunction(readSt.isUnread)
    server.bindFunction(readSt.setRead)

    server.run(hall.servconf['READST'][1])

    
